package hu.arguscab.ai;

import java.util.Iterator;

/**
 * Egy olyan iterátor, ami csak a legkedvezőbb (legnagyobb Fitness value-val rendelkező)
 * node-okon iterál végig.
 * An iterator which iterate only on the Nodes that have the biggest Fitness Value.<br>
 * &nbsp&nbsp&nbsp 12       Order of the iteration is: A, B, C, D, E, F, G<br>
 *&nbsp&nbsp/ &nbsp \<br>
 *&nbsp12 &nbsp &nbsp &nbsp9<br>
 * / \ &nbsp &nbsp/ &nbsp\<br>
 *     12&nbsp&nbsp3&nbsp&nbsp8  9<br>
 * @param <E>
 * @author sharp
 */
public class BestFirstSearchIterator<E> extends TreeIterable<E, Model>{

	public BestFirstSearchIterator(Tree<E, Model> graph) {
		super(graph);
	}

	@Override
	public Iterator<Node<Model>> iterator() {
		return new BestFirstSearchIteratorImpl<E>(getTree());
	}

}

/**
 * @TODO: Egy uj class: Fitnessable. Model = Fitnessable + State. Ez az iterátor
 * Fitnessable node-okon járjon keresztül.
 * @author sharp
 * @param <E>
 */
class BestFirstSearchIteratorImpl<E> implements Iterator<Node<Model>> {

	Node<Model> current;
	Tree<E, Model> tree;

	public BestFirstSearchIteratorImpl(Tree<E, Model> graph) {
		this.tree = graph;
		
	}

	@Override
	public boolean hasNext() {
		//List<Node<Model>> asd = tree.getChilds(current);
		if (current == null)	// az első lekérdezéskor
			return true;
		return tree.hasChild(current);
	}

	@Override
	public Node<Model> next() {
		if (current == null) {
			current = tree.getRoot();
			return current;
		}
		Node<Model> max = null;
		float f = -Float.MAX_VALUE;
		//List<Node<Model>> asd = tree.getChilds(current);
		for ( Node<Model> child : tree.childs(current) ) {
			Model m = child.getData();
			if ( m.getFitness() > f ) {
				f = m.getFitness();
				max = child;
			}
		}
		assert max != null;
		current = max;
		return current;
	}

	@Override
	public void remove() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

}
